home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Medal Software 5
/
Gold Medal Software - Volume 5 (Gold Medal) (1995).iso
/
windows
/
win31
/
cenviw.arj
/
DDE.LIB
< prev
next >
Wrap
Text File
|
1994-03-08
|
8KB
|
244 lines
// DDE.lib - Library for common DDE functions.
// ver.1
//
//***** struct ddeSession: Structure to define a session
// Many of the following functions refer to the parameters of a dde
// session in some or all elements of a ddeSession structure, which
// contains the following members (along with other non-documented
// members that should be of no concern):
// .Application: string - DDE Server application name
// .Topic: string - DDE Server string
// .hwndClient: int - Window handle of the DDE client
// .hwndServer: int - Window handle of the DDE server
// .TerminateFunction: string - function to call when other party terminates
//
//
//***** ServerDirectory(): Get the handle for this window
// SYNTAX: int ServerDirectory(string Application,string Topic,struct[] ServerList)
// WHERE: Application: DDE Server application string, or NULL for ALL
// Topic: DDE Server Topic, or NULL for ALL
// ServerList: array created for each entry. Array elements are:
// .Application - Returned application name
// .Topic - Returned Topic name
// RETURN: Count of elements in ServerList
// MODIFY: Modifies ServerList to be an array. Undefined if returns 0.
//
//
#include <Window.lib>
//*************************************************************
//*************** Globals and Defines Used ********************
//*************************************************************
DDELibMainHwnd;
#define DEFAULT_TIMEOUT 10000 // timeout if none supplied to functions
#define WM_DDE_INITIATE 0x03E0
#define WM_DDE_TERMINATE 0x03E1
#define WM_DDE_ADVISE 0x03E2
#define WM_DDE_UNADVISE 0x03E3
#define WM_DDE_ACK 0x03E4
#define WM_DDE_DATA 0x03E5
#define WM_DDE_REQUEST 0x03E6
#define WM_DDE_POKE 0x03E7
#define WM_DDE_EXECUTE 0x03E8
#define WM_DESTROY 0x0002
#define CF_TEXT 1
#define GMEM_MOVEABLE 0x0002
#define GMEM_DDESHARE 0x2000
//*****************************************************************
//***** Main DDE window, for finding servers, and being found *****
//*****************************************************************
DDEGettingServerDirectory = False;
DDEServerDirectory;
DDELibMainHwnd = MakeWindow(NULL,NULL,"DDELibMainFunction","CEnvi DDE Library",
WS_POPUP,0,0,0,0,NULL);
assert( NULL != DDELibMainHwnd );
DDELibMainFunction(hwnd,msg,wParm,lParm)
{
switch ( msg ) {
case WM_DDE_ACK:
if ( DDEGettingServerDirectory ) {
// Save application and topic in our growing array
tAtomApp = LoWord(lParm);
tAtomTop = HiWord(lParm);
tIndex = defined(DDEServerDirectory)
? 1 + GetArraySpan(DDEServerDirectory) : 0 ;
DDEServerDirectory[tIndex].Application = GetAtomString(tAtomApp);
DDEServerDirectory[tIndex].Topic = GetAtomString(tAtomTop);
// Tell server to terminate this connection
PostMessage(wParm,WM_DDE_TERMINATE,hwnd,0);
return(0);
}
break;
}
}
ServerDirectory(pApplication,pTopic,pServerList)
{
// Create atom for Server and Topic (0 if NULL strings)
sdAtomApp = pApplication ? GlobalAddAtom(pApplication) : 0 ;
sdAtomTop = pTopic ? GlobalAddAtom(pTopic) : 0 ;
// Send message to all windows to initiate DDE connection
undefine(pServerList);
Multitask(False);
DDEGettingServerDirectory = True;
SendMessage(0xFFFF,WM_DDE_INITIATE,DDELibMainHwnd,sdAtomApp,sdAtomTop);
DDEGettingServerDirectory = False;
Multitask(True);
// undo atoms that were made
if ( sdAtomApp ) GlobalDeleteAtom(sdAtomApp);
if ( sdAtomTop ) GlobalDeleteAtom(sdAtomTop);
// DDEServerDirectory will contain all responses
if ( defined(DDEServerDirectory) ) {
pServerList = DDEServerDirectory;
undefine(DDEServerDirectory);
return 1 + GetArraySpan(pServerList);
} else {
return 0;
}
}
//******************************************************
//*************** Utility functions ********************
//******************************************************
DoPostedDDEMessage(pddeSession,pTimeout) // DoWindows until Postedmessage is null or timeout
{ // -1 to never timeout
if ( -1 != pTimeout )
tEndTime = clock() + pTimeout * CLOCKS_PER_SEC / 1000;
while( 0 != pddeSession.PostedMessage
&& ( -1 == pTimeout || clock() < tEndTime )
&& DoWindows(True) ) ;
}
SendMessage(Window,Message,wParam,lParamLo,lParamHi)
{
if ( va_arg() == 4 )
return(SendMessage(Window,Message,wParam,lParamLo & 0xFFFF,(lParamLo >> 16) & 0xFFFF));
return( DynamicLink("USER","SENDMESSAGE",UWORD32,PASCAL,
Window,Message,wParam,lParamHi,lParamLo) );
}
PostMessage(Window,Message,wParam,lParamLo,lParamHi)
{
if ( va_arg() == 4 )
return(PostMessage(Window,Message,wParam,lParamLo & 0xFFFF,(lParamLo >> 16) & 0xFFFF));
return( DynamicLink("USER","POSTMESSAGE",SWORD16,PASCAL,
Window,Message,wParam,lParamHi,lParamLo) );
}
GlobalAddAtom(pAtomString)
{
return DynamicLink("USER","GLOBALADDATOM",UWORD16,PASCAL,pAtomString);
}
GlobalDeleteAtom(pAtom)
{
return DynamicLink("USER","GLOBALDELETEATOM",UWORD16,PASCAL,pAtom);
}
LoWord(dword)
{
return dword & 0xFFFF
}
HiWord(dword)
{
return (dword >> 16) & 0xFFFF
}
GetAtomString(pAtom,pAtomBuffer,pAtomBufferLength)
{
#define MAX_ATOM_BUFFER_SIZE 50
lAtomBuffer[MAX_ATOM_BUFFER_SIZE-1] = '\0';
if ( 0 == DynamicLink("USER","GLOBALGETATOMNAME",UWORD16,PASCAL,
pAtom,lAtomBuffer,MAX_ATOM_BUFFER_SIZE) )
return("");
strcpy(lString,lAtomBuffer);
return lString;
}
GlobalLock(pMemoryHandle) // return locked pointer to memory
{
return DynamicLink("KERNEL","GLOBALLOCK",UWORD32,PASCAL,pMemoryHandle);
}
GlobalUnlock(pMemoryHandle)
{
return DynamicLink("KERNEL","GLOBALUNLOCK",SWORD16,PASCAL,pMemoryHandle);
}
GlobalAlloc(pFlags,pSize)
{
return DynamicLink("KERNEL","GLOBALALLOC",UWORD16,PASCAL,
pFlags,HiWord(pSize),LoWord(pSize));
}
GlobalFree(pMemoryHandle)
{
return DynamicLink("KERNEL","GLOBALFREE",UWORD16,PASCAL,pMemoryHandle);
}
GlobalSize(pMemoryHandle)
{
return DynamicLink("KERNEL","GLOBALSIZE",UWORD32,PASCAL,pMemoryHandle);
}
DdeAckToWord(pDdeAck) // convert DdeAck structure to a word
{
lWord = pDdeAck.bAppReturnedCode;
if ( pDdeAck.fBusy ) lWord |= 0x40;
if ( pDdeAck.fAck ) lWord |= 0x80;
}
WordToDdeAck(pWord) // convert word to DdeAck structure
{
lDdeAck.bAppReturnedCode = pWord & 0xFF;
lDdeAck.fBusy = (pWord >> 14) & 1;
lDdeAck.fAck = (pWord >> 15) & 1;
return lDdeAck;
}
DdeAdviseStuctureToHandle(pDdeAdvise)
{
lDWord = pDdeAdvise.cfFormat << 16;
if ( pDdeAdvise.fDeferUpd ) lDWord |= 0x40;
if ( pDdeAdvise.fAckReq ) lDWord |= 0x80;
assert( lhDdeAdvise = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,4) );
poke(GlobalLock(lhDdeAdvise),lDWord,UWORD32);
GlobalUnlock(lhDdeAdvise);
return lhDdeAdvise;
}
DdeDataHandleToStructure(hDdeData)
{
lpDdeData = GlobalLock(hDdeData);
lBits = peek(lpDdeData+1);
lDdeData.fResponse = (lBits >> 4) & 1;
lDdeData.fRelease = (lBits >> 5) & 1;
lDdeData.fAckReq = (lBits >> 7) & 1;
if ( CF_TEXT == (lDdeData.cfFormat = peek(lpDdeData+2,SWORD16)) ) {
// read in buffer up to NULL
lDdeData.Value = peek(lpDdeData+4,GlobalSize(hDdeData)-4);
SetArraySpan(lDdeData.Value,strlen(lDdeData.Value));
}
GlobalUnlock(hDdeData);
return lDdeData;
}